/*
 *  Copyright 2009 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.OutputUtilities;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;

/**
 * 
 * @author Jeff Butler
 * 
 */
public class BatchInsertElementGenerator extends AbstractXmlElementGenerator {

	public BatchInsertElementGenerator() {
		super();
	}

	@Override
	public void addElements(XmlElement parentElement) {
		XmlElement answer = new XmlElement("insert"); //$NON-NLS-1$

		answer.addAttribute(new Attribute("id", introspectedTable.getBatchInsertStatementId())); //$NON-NLS-1$
		answer.addAttribute(new Attribute("parameterType", "java.util.List")); //$NON-NLS-1$

		context.getCommentGenerator().addComment(answer);

		/**去掉主键校验，如果主键为系统自动生成，则去掉该注释，如果主键为数据库自动生成，刚注释该段
		GeneratedKey gk = introspectedTable.getGeneratedKey();
		if (gk != null) {
			IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
			if (introspectedColumn != null) {
				if (gk.isJdbcStandard()) {
					answer.addAttribute(new Attribute("useGeneratedKeys", "true")); //$NON-NLS-1$ //$NON-NLS-2$
					answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
				} else {
					answer.addElement(getSelectKey(introspectedColumn, gk));
				}
			}
		}
		 */
		StringBuilder insertClause = new StringBuilder();
		StringBuilder valuesClause = new StringBuilder();

		insertClause.append("insert into "); //$NON-NLS-1$
		insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
		insertClause.append(" ("); //$NON-NLS-1$

		valuesClause.append(" ("); //$NON-NLS-1$

		List<String> valuesClauses = new ArrayList<String>();
		Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns().iterator();
		while (iter.hasNext()) {
			IntrospectedColumn introspectedColumn = iter.next();
			if (introspectedColumn.isIdentity()) {
				// cannot set values on identity fields
				continue;
			}

			insertClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
			valuesClause.append(MyBatis3FormattingUtilities.getBatchParameterClause(introspectedColumn));
			if (iter.hasNext()) {
				insertClause.append(", "); //$NON-NLS-1$
				valuesClause.append(", "); //$NON-NLS-1$
			}
			/**
			if (valuesClause.length() > 80) {
				answer.addElement(new TextElement(insertClause.toString()));
				insertClause.setLength(0);
				OutputUtilities.xmlIndent(insertClause, 1);

				valuesClauses.add(valuesClause.toString());
				valuesClause.setLength(0);
				OutputUtilities.xmlIndent(valuesClause, 1);
			}**/
		}

		insertClause.append(')');
		insertClause.append(" values ");
		
		answer.addElement(new TextElement(insertClause.toString()));

		valuesClause.append(')');
		valuesClauses.add(valuesClause.toString());
		
		
		XmlElement foreachElement = new XmlElement("foreach"); //$NON-NLS-1$
		foreachElement.addAttribute(new Attribute("collection", "records"));
		foreachElement.addAttribute(new Attribute("item", "item"));
		foreachElement.addAttribute(new Attribute("separator", ","));
		foreachElement.addElement(new TextElement(valuesClause.toString())); //$NON-NLS-1$
		answer.addElement(foreachElement);
		

		if (context.getPlugins().sqlMapInsertElementGenerated(answer, introspectedTable)) {
			parentElement.addElement(answer);
		}
	}
}
